local extension = Package:new("wei")
extension.extensionName = "klxf"
-- extension.game_modes_whitelist = { 'kl_heg_mode' }

local heg_mode = require "packages.klxf.kl__hegemony"
extension:addGameMode(heg_mode)

local H = require "packages/klxf/util"
local U = require "packages/utility/utility"

Fk:loadTranslationTable {
  ["wei"] = "魏",
  ["kl"] = "抗绿",
}

local wangji = General(extension, "kl__wangji", "wei", 3)
local kl__qizhi = fk.CreateTriggerSkill {
  name = "kl__qizhi",
  anim_type = "control",
  events = { fk.TargetSpecified },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and --player.phase ~= Player.NotActive and
        data.firstTarget                                  --and data.card.type ~= Card.TypeEquip
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(table.filter(room.alive_players, function(p)
      return not p:isNude() and not table.contains(AimGroup:getAllTargets(data.tos), p.id)
    end), Util.IdMapper)
    if #targets == 0 then return end
    local tos = room:askForChoosePlayers(player, targets, 1, 1, "#kl__qizhi-choose", self.name, true)
    if #tos > 0 then
      self.cost_data = { tos = tos }
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "@kl__qizhi-turn", 1)
    local to = room:getPlayerById(self.cost_data.tos[1])
    local id = room:askForCardChosen(player, to, "he", self.name)
    room:throwCard({ id }, self.name, to, player)
    if not to.dead then
      to:drawCards(1, self.name)
    end
  end,
}
local kl__jinqu = fk.CreateTriggerSkill {
  name = "kl__jinqu",
  anim_type = "drawcard",
  events = { fk.EventPhaseStart },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Finish
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    -- 摸两张牌
    player:drawCards(2, self.name)

    -- 计算需要弃牌的数量
    local n = #player:getCardIds("h") - player:usedSkillTimes("kl__qizhi", Player.HistoryTurn)
    if n > 0 then
      -- 弃牌至 X 张（X 为本回合发动〖奇制〗的次数）
      room:askForDiscard(player, n, n, false, self.name, false)
    end
  end,
}
wangji:addSkill(kl__qizhi)
wangji:addSkill(kl__jinqu)
Fk:loadTranslationTable {
  ["kl__wangji"] = "王基",
  ["#kl__wangji"] = "经行合一",
  ["designer:kl__wangji"] = "官方&抗绿先锋",

  ["kl__qizhi"] = "奇制",
  [":kl__qizhi"] = "当你使用牌指定目标后，你可弃置一名不为目标的角色的一张牌，然后令其摸一张牌。",
  ["kl__jinqu"] = "进趋",
  [":kl__jinqu"] = "结束阶段，你可摸两张牌，然后将手牌弃至X张（X为你本回合发动〖奇制〗的次数）。",
  ["@kl__qizhi-turn"] = "奇制",
  ["#kl__qizhi-choose"] = "奇制：你可弃置一名角色一张牌，然后其摸一张牌",

  ["$kl__qizhi1"] = "声东击西，敌寇一网成擒。",
  ["$kl__qizhi2"] = "吾意不在此地，已遣别部出发。",
  ["$kl__jinqu1"] = "建上昶水城，以逼夏口！",
  ["$kl__jinqu2"] = "通川聚粮，伐吴之业，当步步为营。",
  ["~kl__wangji"] = "天下之势，必归大魏，可恨，未能得见呐！",
}

local caocao = General(extension, "kl__caocao", "wei", 4)
local kl__hujia = fk.CreateTriggerSkill {
  name = "kl__hujia",
  anim_type = "defensive",
  events = { fk.DamageInflicted },
  can_trigger = function(self, event, target, player, data)
    return target == player
        and player:hasSkill(self)
        and not table.every(player.room:getOtherPlayers(player), function(p) return p.kingdom ~= player.kingdom end)
        and player.kingdom ~= "unknown"
        and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if p:isAlive() and p.kingdom == player.kingdom then
        if room:askForSkillInvoke(p, self.name, data, "#kl__hujia-ask::" .. player.id) then
          room:damage {
            from = data.from,
            to = p,
            damage = data.damage,
            damageType = data.damageType,
            skillName = self.name,
            chain = data.chain,
            card = data.card,
          }
          return true
        end
      end
    end
  end,
}
local kl__jianxiong = fk.CreateTriggerSkill {
  name = "kl__jianxiong",
  anim_type = "masochism",
  events = { fk.Damaged },
  on_use = function(self, event, target, player, data)
    if data.card and U.hasFullRealCard(player.room, data.card) then
      player.room:obtainCard(player.id, data.card, true, fk.ReasonJustMove)
    end
    player:drawCards(1, self.name)
  end,
}
caocao:addSkill(kl__jianxiong)
caocao:addSkill(kl__hujia)
Fk:loadTranslationTable {
  ["kl__caocao"] = "曹操",
  ["#kl__caocao"] = "魏武帝",
  ["designer:kl__caocao"] = "官方&抗绿先锋",

  ["kl__jianxiong"] = "奸雄",
  [":kl__jianxiong"] = "当你受到1点伤害后，你可获得对你造成伤害的牌，然后摸一张牌。",
  ["kl__hujia"] = "护驾",
  [":kl__hujia"] = "每回合限一次，当你受到伤害时，你可令与你势力相同的其他角色依次选择是否将此伤害转移给其。",
  ["#kl__hujia-ask"] = "护驾：是否将曹操受到的伤害转移给你",

  ["$kl__jianxiong1"] = "宁教我负天下人，休教天下人负我！",
  ["$kl__jianxiong2"] = "吾好梦中杀人！",
  ["$kl__hujia1"] = "来人！护驾！",
  ["$kl__hujia2"] = "魏将何在？",
  ["~kl__caocao"] = "霸业未成！未成啊！",
}

local caopi = General(extension, "kl__caopi", "wei", 3)
local kl__fangzhu = fk.CreateTriggerSkill {
  name = "kl__fangzhu",
  anim_type = "masochism",
  events = { fk.Damaged },
  on_cost = function(self, event, target, player, data)
    local to = player.room:askForChoosePlayers(player,
      table.map(player.room:getOtherPlayers(player, false), Util.IdMapper)
      , 1, 1, "#kl__fangzhu-choose:::" .. player:getLostHp(), self.name, true)
    if #to > 0 then
      self.cost_data = { tos = to }
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local to = player.room:getPlayerById(self.cost_data.tos[1])
    to:turnOver()
    if not to.dead then
      to:drawCards(2, self.name) -- 固定摸两张牌
    end
  end,
}
local kl__xingshang = fk.CreateTriggerSkill {
  name = "kl__xingshang",
  anim_type = "drawcard",
  events = { fk.Death },
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and not target:isNude()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards_id = target:getCardIds { Player.Hand, Player.Equip }
    room:obtainCard(player.id, cards_id, false, fk.ReasonPrey)
  end,
}
caopi:addSkill(kl__xingshang)
caopi:addSkill(kl__fangzhu)
Fk:loadTranslationTable {
  ["kl__caopi"] = "曹丕",
  ["#kl__caopi"] = "霸业的继承者",
  ["designer:kl__caopi"] = "官方",

  ["kl__xingshang"] = "行殇",
  [":kl__xingshang"] = "当其他角色死亡时，你可获得其所有牌。",
  ["kl__fangzhu"] = "放逐",
  [":kl__fangzhu"] = "当你受到伤害后，你可令一名其他角色翻面，然后其摸两张牌。",
  ["#kl__fangzhu-choose"] = "放逐：你可令一名其他角色翻面，然后其摸两张牌",

  ["$kl__xingshang1"] = "我的是我的，你的还是我的。",
  ["$kl__xingshang2"] = "来，管杀还管埋！",
  ["$kl__fangzhu1"] = "死罪可免，活罪难赦！",
  ["$kl__fangzhu2"] = "给我翻过来！",
  ["~kl__caopi"] = "子建，子建……",
}

local caomao = General(extension, "kl__caomao", "wei", 3)
-- local kl__qianlong = fk.CreateTriggerSkill {
--   name = "kl__qianlong",
--   anim_type = "masochism",
--   events = { fk.Damaged },
--   can_trigger = function(self, event, target, player, data)
--     return target == player and target:hasSkill(self)
--   end,
--   on_trigger = function(self, event, target, player, data)
--     self.cancel_cost = false
--     for i = 1, data.damage do
--       if i > 1 and (self.cancel_cost or not player:hasSkill(self)) then break end
--       self:doCost(event, target, player, data)
--     end
--   end,
--   on_cost = function(self, event, target, player, data)
--     local room = player.room
--     if room:askForSkillInvoke(player, self.name, data, "#kl__qianlong-invoke") then
--       return true
--     end
--     self.cancel_cost = true
--   end,
--   on_use = function(self, event, target, player, data)
--     local room = player.room
--     local lostHp = player:getLostHp()
--     local cards = U.turnOverCardsFromDrawPile(player, lostHp, self.name)
--     local result = room:askForGuanxing(player, cards, { 0, lostHp }, { 0, lostHp }, self.name, true,
--       { "Bottom", "toObtain" })
--     if #result.bottom > 0 then
--       room:moveCardTo(result.bottom, Player.Hand, player, fk.ReasonJustMove, self.name, "", true, player.id)
--     end
--     U.returnCardsToDrawPile(player, result.top, self.name, false, false)
--   end,
-- }
local kl__qianlong = fk.CreateTriggerSkill {
  name = "kl__qianlong",
  anim_type = "masochism",
  events = { fk.Damaged },
  can_trigger = function(self, event, target, player, data)
    return target == player and target:hasSkill(self)
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for i = 1, data.damage do
      if i > 1 and (self.cancel_cost or not player:hasSkill(self)) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askForSkillInvoke(player, self.name, data, "#kl__qianlong-invoke") then
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = U.turnOverCardsFromDrawPile(player, 2, self.name)
    local result = room:askForGuanxing(player, cards, { 0, 2 }, { 0, 2 }, self.name, true,
      { "Bottom", "toObtain" })
    if #result.bottom > 0 then
      room:moveCardTo(result.bottom, Player.Hand, player, fk.ReasonJustMove, self.name, "", true, player.id)
    end
    U.returnCardsToDrawPile(player, result.top, self.name, false, false)
  end,
}
local kl__juetao = fk.CreateActiveSkill {
  name = "kl__juetao",
  anim_type = "offensive",
  frequency = Skill.Limited,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 0,
  prompt = "#kl__juetao",
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    while not player.dead do
      local cards = room:getNCards(1, "bottom")
      room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, self.name, nil, true, player.id)
      if not U.askForUseRealCard(room, player, cards, nil, self.name,
            "#kl__juetao-use:::" .. Fk:getCardById(cards[1]):toLogString(), {
              expand_pile = cards,
              bypass_times = true,
              extraUse = true
            }) then
        room:moveCards({
          ids = cards,
          toArea = Card.DrawPile,
          moveReason = fk.ReasonJustMove,
          skillName = self.name,
        })
        break
      end
    end
  end,
}
caomao:addSkill(kl__juetao)
caomao:addSkill(kl__qianlong)
Fk:loadTranslationTable {
  ["kl__caomao"] = "曹髦",
  ["#kl__caomao"] = "霸业的终耀",
  ["designer:kl__caomao"] = "官方&抗绿先锋",

  ["kl__qianlong"] = "潜龙",
  -- [":kl__qianlong"] = "当你受到1点伤害后，你可展示牌堆顶X张牌并获得其中任意张牌，然后将剩余的牌置于牌堆底（X为你已损失的体力值）。",
  [":kl__qianlong"] = "当你受到1点伤害后，你可展示牌堆顶两张牌并获得其中任意张牌，然后将剩余的牌置于牌堆底。",
  ["kl__juetao"] = "决讨",
  [":kl__juetao"] = "限定技，出牌阶段，你可展示牌堆底牌并使用之。你可重复此流程直到牌堆底牌不能被使用（以此法使用的杀无次数限制）。",
  ["#kl__juetao-use"] = "决讨：是否使用此牌？",
  ["#kl__juetao"] = "决讨：你可重复展示牌堆底牌并使用之。",
  ["kl__juetao_viewas"] = "决讨",
  ["#kl__qianlong-guanxing"] = "潜龙：获得其中至多%arg张牌（获得上方的牌，下方的牌置于牌堆底）",
  ["kl__qianlong_get"] = "获得",
  ["kl__qianlong_bottom"] = "置于牌堆底",
  ["#kl__qianlong-invoke"] = "是否发动 潜龙？",

  ["$kl__qianlong1"] = "鸟栖于林，龙潜于渊。",
  ["$kl__qianlong2"] = "游鱼惊钓，潜龙飞天。",
  ["$kl__juetao1"] = "登车拔剑起，奋跃搏乱臣！",
  ["$kl__juetao2"] = "陵云决心意，登辇讨不臣！",
  ["~kl__caomao"] = "宁作高贵乡公死，不作汉献帝生……",
}

local caorui = General(extension, "kl__caorui", "wei", 3)
local kl__mingjian = fk.CreateTriggerSkill {
  name = "kl__mingjian",
  anim_type = "support",
  events = { fk.EventPhaseEnd },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self.name) and
        player.phase == Player.Play and not player:isKongcheng()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    -- 修复点1：确保targets是ID列表
    local targets = table.map(
      table.filter(room:getOtherPlayers(player), function(p) return not p.dead end),
      function(p) return p.id end
    )
    if #targets == 0 then return false end

    -- 修复点2：显式指定所有可选参数
    local result = room:askForChoosePlayers(
      player,              -- 询问对象
      targets,             -- 可选目标列表(ID)
      1,                   -- 最少选择
      1,                   -- 最多选择
      "#kl__mingjian-ask", -- 提示语
      self.name,           -- 技能名
      true,                -- 可取消
      false                -- 无默认指示
    )
    if #result > 0 then
      self.cost_data = result[1]
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    local handcards = player:getCardIds(Player.Hand)

    -- 转移所有手牌
    room:moveCardTo(handcards, Player.Hand, to, fk.ReasonGive, self.name)

    -- 添加回合结束时的额外回合（正确方法）
    to:gainAnExtraTurn(true, self.name, {
      phase_table = { Player.Play }, -- 仅包含出牌阶段
      skip_phase = {
        Player.Start,
        Player.Judge,
        Player.Draw,
        Player.Discard,
        Player.Finish
      }
    })
  end
}
local kl__huituo = fk.CreateTriggerSkill {
  name = "kl__huituo",
  anim_type = "masochism",
  events = { fk.Damaged },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for i = 1, data.damage do
      if i > 1 and (self.cancel_cost or not player:hasSkill(self)) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local to = player.room:askForChoosePlayers(player, table.map(player.room:getAlivePlayers(), Util.IdMapper),
      1, 1, "#kl__huituo-choose", self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    local judge = {
      who = to,
      reason = self.name,
      pattern = ".",
    }
    room:judge(judge)
    if judge.card.color == Card.Red then
      if to:isWounded() then
        room:recover({
          who = to,
          num = 1,
          recoverBy = player,
          skillName = self.name
        })
      end
    elseif judge.card.color == Card.Black then
      to:drawCards(1, self.name)
    end
  end,
}
caorui:addSkill(kl__huituo)
caorui:addSkill(kl__mingjian)
Fk:loadTranslationTable {
  ["kl__caorui"] = "曹叡",
  ["#kl__caorui"] = "天姿的明君",
  ["designer:kl__caorui"] = "官方&抗绿先锋",

  ["kl__mingjian"] = "明鉴",
  [":kl__mingjian"] = "出牌阶段结束时，你可将所有手牌交给一名角色。若如此做，本回合结束时，其执行一个仅包含出牌阶段的额外回合。",
  ["#kl__mingjian-ask"] = "明鉴：选择要交给的角色",
  ["kl__huituo"] = "恢拓",
  [":kl__huituo"] = "当你受到1点伤害后，你可令一名角色判定。若结果为：红色，其回复1点体力；黑色，其摸一张牌。",
  ["#kl__huituo-choose"] = "恢拓：选择一名角色进行判定",

  ["$kl__huituo1"] = "大展宏图，就在今日！",
  ["$kl__huituo2"] = "富我大魏，扬我国威！",
  ["$kl__mingjian1"] = "你我推心置腹，岂能相负。",
  ["$kl__mingjian2"] = "孰忠孰奸，朕尚能明辨！",
  ["~kl__caorui"] = "悔不该耽于逸乐，至有今日……",
}

local zhenji = General(extension, "kl__zhenji", "wei", 3, 3, General.Female)
local kl__luoshen = fk.CreateTriggerSkill {
  name = "kl__luoshen",
  anim_type = "drawcard",
  events = { fk.EventPhaseStart },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    while true do
      local judge = {
        who = player,
        reason = self.name,
        pattern = ".|.",
      }
      room:judge(judge)

      -- 新效果：无论颜色都获得判定牌[^2]
      player.room:obtainCard(player.id, judge.card, true)

      -- 根据颜色决定是否继续
      if judge.card.color == Card.Red then
        break -- 红色则结束流程
      else
        -- 黑色时询问是否继续
        if not room:askForSkillInvoke(player, self.name) then
          break
        end
      end
    end
  end,
}
local kl__qingguo = fk.CreateViewAsSkill {
  name = "kl__qingguo",
  anim_type = "defensive",
  pattern = "jink", --  pattern = "slash",
  handly_pile = true,
  card_filter = function(self, to_select, selected)
    if #selected == 1 then return false end
    return Fk:getCardById(to_select).color == Card.Black
    -- and Fk:currentRoom():getCardArea(to_select) == Player.Hand
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then
      return nil
    end
    local c = Fk:cloneCard("jink")
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
}
zhenji:addSkill(kl__luoshen)
zhenji:addSkill(kl__qingguo)
Fk:loadTranslationTable {
  ["kl__zhenji"] = "甄姬",
  ["#kl__zhenji"] = "薄幸的美人",
  ["designer:kl__zhenji"] = "官方&抗绿先锋",

  ["kl__luoshen"] = "洛神",
  [":kl__luoshen"] = "准备阶段，你可判定，然后获得判定牌。若结果为黑色，你可重复此流程。",
  ["kl__qingguo"] = "倾国",
  [":kl__qingguo"] = "你可将一张黑色牌当【闪】使用或打出。",

  ["$kl__luoshen1"] = "髣髴兮若轻云之蔽月。",
  ["$kl__luoshen2"] = "飘飖兮若流风之回雪。",
  ["$kl__qingguo1"] = "凌波微步，罗袜生尘。",
  ["$kl__qingguo2"] = "体迅飞凫，飘忽若神。",
  ["~kl__zhenji"] = "悼良会之永绝兮，哀一逝而异乡。",
}

local dianwei = General(extension, "kl__dianwei", "wei", 4)
local kl__qiangxi = fk.CreateActiveSkill {
  name = "kl__qiangxi",
  anim_type = "offensive",
  max_card_num = 1,
  target_num = 1,
  prompt = "#kl__qiangxi",
  can_use = Util.TrueFunc,
  card_filter = function(self, to_select, selected)
    return Fk:getCardById(to_select).sub_type == Card.SubtypeWeapon and
        not Self:prohibitDiscard(Fk:getCardById(to_select))
  end,
  target_filter = function(self, to_select, selected, selected_cards)
    if #selected == 0
        and to_select ~= Self.id
        and not table.contains(Self:getTableMark("kl__qiangxi-phase"), to_select)
    then
      -- 移除攻击范围/距离检查逻辑
      return true -- 允许选择任意符合条件的角色[^2]
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    room:addTableMark(player, "kl__qiangxi-phase", target.id)
    if #effect.cards > 0 then
      room:throwCard(effect.cards, self.name, player)
    else
      room:loseHp(player, 1, self.name)
    end
    room:damage {
      from = player,
      to = target,
      damage = 1,
      skillName = self.name,
    }
  end,
}
local kl__kangyong = fk.CreateTriggerSkill {
  name = "kl__kangyong",
  anim_type = "defensive",
  frequency = Skill.Compulsory, -- 锁定技标记[^2]
  events = { fk.TurnStart, fk.TurnEnd },

  -- 无条件触发判断
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) -- 移除原有条件判断[^3]
  end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      local n = player.maxHp - player.hp -- 直接计算需要回复的数值[^4]
      room:setPlayerMark(player, "kl__kangyong-turn", n)
      room:recover({                     -- 强制回复到体力上限
        who = player,
        num = n,
        skillName = self.name
      })
    else
      local n = player:getMark("kl__kangyong-turn")
      room:loseHp(player, n, self.name) -- 强制失去对应体力[^5]
    end
  end,
}
dianwei:addSkill(kl__qiangxi)
dianwei:addSkill(kl__kangyong)
Fk:loadTranslationTable {
  ["kl__dianwei"] = "典韦",
  ["#kl__dianwei"] = "古之恶来",
  ["designer:kl__dianwei"] = "官方&抗绿先锋",

  ["kl__qiangxi"] = "强袭",
  [":kl__qiangxi"] = "出牌阶段对每名角色限一次，你可失去1点体力或弃置一张武器牌，对一名其他角色造成1点伤害。",
  ["#kl__qiangxi"] = "强袭：弃置一张武器牌，或点\"确定\"失去1点体力，对一名本阶段未选择过的角色造成1点伤害",
  ["kl__kangyong"] = "亢勇",
  [":kl__kangyong"] = "锁定技，回合开始时，你回复体力至体力上限；回合结束时，你失去等量体力。",

  ["$kl__qiangxi1"] = "吃我一戟！",
  ["$kl__qiangxi2"] = "看我三步之内取你小命！",
  ["$kl__kangyong1"] = "此猛士之血，其与醇酒孰烈乎？",
  ["$kl__kangyong2"] = "歃血为誓，城在则人在！",
  ["~kl__dianwei"] = "主公，快走……！",
}

local xunyou = General(extension, "kl__xunyou", "wei", 3)
local kl__zhiyu = fk.CreateTriggerSkill {
  name = "kl__zhiyu",
  anim_type = "masochism",
  events = { fk.Damaged },
  can_trigger = function(self, event, target, player, data)
    return target == player and data.damage > 0 and player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local from = data.from

    -- 摸牌展示阶段修正
    player:drawCards(1, self.name)
    local cards = player:getCardIds("h")
    player:showCards(cards)

    -- 颜色判断逻辑优化
    local valid = #cards == 0 or table.every(cards, function(id)
      return Fk:getCardById(id).color == Fk:getCardById(cards[1]).color
    end)

    -- 增加主动确认机制
    if valid and from and not from.dead and not from:isKongcheng() then
      local choice = room:askForChoice(player, { "yes", "no" }, self.name, "#kl__zhiyu-discard")
      if choice == "yes" then
        local toDiscard = room:askForCardChosen(player, from, "he", self.name)
        if toDiscard ~= -1 then
          room:throwCard(toDiscard, self.name, from, player) -- 主动弃置流程[^4]
        end
      end
    end
  end
}
local kl__qice = fk.CreateViewAsSkill {
  name = "kl__qice",
  pattern = ".",
  prompt = "#kl__qice-card",
  interaction = function()
    local all_names = U.getAllCardNames("t")
    return U.CardNameBox {
      choices = U.getViewAsCardNames(Self, "kl__qice", all_names),
      all_choices = all_names,
      default_choice = "AskForCardsChosen"
    }
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    if Fk.all_card_types[self.interaction.data] == nil then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(Self:getCardIds(Player.Hand))
    card.skillName = self.name
    return card
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name) == 0 and not player:isKongcheng()
  end,
}
xunyou:addSkill(kl__qice)
xunyou:addSkill(kl__zhiyu)
Fk:loadTranslationTable {
  ["kl__xunyou"] = "荀攸",
  ["#kl__xunyou"] = "曹魏的谋主",
  ["designer:kl__xunyou"] = "官方&抗绿先锋",

  ["kl__qice"] = "奇策",
  [":kl__qice"] = "每回合限一次，你可将所有手牌当任意一张普通锦囊牌使用。",
  ["#kl__qice-card"] = "发动 奇策，将所有手牌当任意普通锦囊牌使用",
  ["kl__zhiyu"] = "智愚",
  [":kl__zhiyu"] = "当你受到1点伤害后，你可摸一张牌，展示所有手牌。若颜色均相同，你弃置来源一张牌。",
  ["#kl__zhiyu-discard"] = "智愚：你是否弃置来源一张牌？",


  ["$kl__qice1"] = "倾力为国，算无遗策。",
  ["$kl__qice2"] = "奇策在此，谁与争锋？",
  ["$kl__zhiyu1"] = "大勇若怯，大智如愚。",
  ["$kl__zhiyu2"] = "愚者既出，智者何存？",
  ["~kl__xunyou"] = "主公，臣下……先行告退……",
}

local xunyu = General(extension, "kl__xunyu", "wei", 3)
local kl__quhu = fk.CreateActiveSkill {
  name = "kl__quhu",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return not player:isKongcheng() and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return #selected == 0 and Self:canPindian(target) and target.hp > Self.hp
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local pindian = player:pindian({ target }, self.name)
    if pindian.results[target.id].winner == player then
      local targets = {}
      for _, p in ipairs(room:getOtherPlayers(target)) do
        if target:inMyAttackRange(p) then
          table.insert(targets, p.id)
        end
      end
      if #targets == 0 then return end
      local tos = room:askForChoosePlayers(player, targets, 1, 1, "#kl__quhu-choose", self.name)
      room:damage {
        from = target,
        to = room:getPlayerById(tos[1]),
        damage = 1,
        skillName = self.name,
      }
    else
      room:damage {
        from = target,
        to = player,
        damage = 1,
        skillName = self.name,
      }
    end
  end,
}
local kl__jieming = fk.CreateTriggerSkill {
  name = "kl__jieming",
  anim_type = "masochism",
  events = { fk.Damaged }, -- 移除死亡触发时机[^2]

  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,

  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    -- 保持多点伤害处理逻辑[^3]
    for i = 1, data.damage do
      if i > 1 and (self.cancel_cost or not player:hasSkill(self)) then break end
      self:doCost(event, target, player, data)
    end
  end,

  on_cost = function(self, event, target, player, data)
    local to = player.room:askForChoosePlayers(
      player,
      table.map(player.room:getAlivePlayers(), Util.IdMapper),
      1, 1,
      "#kl__jieming-choose",
      self.name,
      true
    )
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
    self.cancel_cost = true
  end,

  on_use = function(self, event, target, player, data)
    local to = player.room:getPlayerById(self.cost_data)
    local x = math.min(to.maxHp, 5) -- 体力上限限制[^4]

    to:drawCards(x, self.name)
    if to.dead then return end

    local handNum = #to:getCardIds("h")
    if handNum > x then
      player.room:askForDiscard(
        to, handNum - x, handNum - x,
        false, self.name, false,
        ".", "#kl__jieming-discard:::" .. (handNum - x)
      )
    end
  end
}
xunyu:addSkill(kl__quhu)
xunyu:addSkill(kl__jieming)
Fk:loadTranslationTable {
  ["kl__xunyu"] = "荀彧",
  ["#kl__xunyu"] = "王佐之才",
  ["designer:kl__xunyu"] = "官方&抗绿先锋",

  ["kl__quhu"] = "驱虎",
  [":kl__quhu"] = "出牌阶段限一次，你可以与一名体力值大于你的角色拼点。若你赢，该角色对其攻击范围内你指定的另一名角色造成1点伤害；若你没赢，其对你造成1点伤害。",
  ["#kl__quhu-choose"] = "驱虎：选择其攻击范围内的一名角色，其对此角色造成1点伤害",
  ["kl__jieming"] = "节命",
  [":kl__jieming"] = "当你受到1点伤害后，你可令一名角色摸X张牌，其将手牌弃置至X张。（X为其体力上限且至多为5）",
  ["#kl__jieming-choose"] = "节命：你可令一名角色摸X张牌并将手牌弃至X张（X为其体力上限且至多为5）",
  ["#kl__jieming-discard"] = "节命：选择%arg张手牌弃置",

  ["$kl__quhu1"] = "此乃驱虎吞狼之计。",
  ["$kl__quhu2"] = "借你之手，与他一搏吧。",
  ["$kl__jieming1"] = "秉忠贞之志，守谦退之节。",
  ["$kl__jieming2"] = "我，永不背弃。",
  ["~kl__xunyu"] = "主公要臣死，臣不得不死。",
}

local zhongyao = General(extension, "kl__zhongyao", "wei", 3)
local kl__zuoding = fk.CreateTriggerSkill {
  name = "kl__zuoding",
  anim_type = "drawcard",
  events = { fk.TargetSpecified },
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and target ~= player and target.phase == Player.Play and data.firstTarget
        and data.card.suit == Card.Spade
        and table.find(AimGroup:getAllTargets(data.tos), function(pid) return not player.room:getPlayerById(pid).dead end) then
      return #player.room.logic:getActualDamageEvents(1, Util.TrueFunc, Player.HistoryPhase) == 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local targets = table.filter(AimGroup:getAllTargets(data.tos),
      function(pid) return not player.room:getPlayerById(pid).dead end)
    local to = player.room:askForChoosePlayers(player, targets, 1, 1, "#kl__zuoding-choose", self.name, true)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:getPlayerById(self.cost_data):drawCards(1, self.name)
  end,
}
local kl__huomo = fk.CreateViewAsSkill {
  name = "kl__huomo",
  pattern = ".|.|.|.|.|basic",
  prompt = function()
    return "#kl__huomo-card"
  end,
  interaction = function()
    local all_names = U.getAllCardNames("b")
    local names = U.getViewAsCardNames(Self, "kl__huomo", all_names, {}, Self:getTableMark("kl__huomo-turn"))
    if #names == 0 then return false end
    return U.CardNameBox { choices = names, all_choices = all_names }
  end,
  card_filter = function(self, to_select, selected)
    local card = Fk:getCardById(to_select)
    return #selected == 0 and card.type ~= Card.TypeBasic and card.color == Card.Black
  end,
  before_use = function(self, player, use)
    local room = player.room
    room:addTableMark(player, "kl__huomo-turn", use.card.trueName)
    local put = use.card:getMark(self.name)
    if put ~= 0 and table.contains(player:getCardIds("he"), put) then
      room:moveCards({
        ids = { put },
        from = player.id,
        toArea = Card.DrawPile,
        moveReason = fk.ReasonPut,
        skillName = self.name,
        proposer = player.id,
        moveVisible = true,
      })
    end
  end,
  view_as = function(self, cards)
    if not self.interaction.data or #cards ~= 1 then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:setMark(self.name, cards[1])
    card.skillName = self.name
    return card
  end,
  enabled_at_play = function(self, player)
    return not player:isNude()
  end,
  enabled_at_response = function(self, player, response)
    return not response and not player:isNude()
  end,
}
zhongyao:addSkill(kl__zuoding)
zhongyao:addSkill(kl__huomo)
Fk:loadTranslationTable {
  ["kl__zhongyao"] = "钟繇",
  ["#kl__zhongyao"] = "正楷萧曹",
  ["designer:kl__zhongyao"] = "官方",

  ["kl__huomo"] = "活墨",
  [":kl__huomo"] = "每回合每种牌名限一次，当你需要使用基本牌时，你可将一张黑色非基本牌置于牌堆顶，视为使用之。",
  ["#kl__huomo-card"] = "活墨：将一张黑色非基本牌置于牌堆顶",
  ["kl__zuoding"] = "佐定",
  [":kl__zuoding"] = "当其他角色于其出牌阶段内使用黑桃牌指定目标后，若本阶段没有角色受到过伤害，你可令一名目标角色摸一张牌。",
  ["#kl__zuoding-choose"] = "佐定：你可令一名目标角色摸一张牌",

  ["$kl__huomo1"] = "笔墨写春秋，挥毫退万敌！",
  ["$kl__huomo2"] = "妙笔在手，研墨在心。",
  ["$kl__zuoding1"] = "只有忠心，没有谋略，是不够的。",
  ["$kl__zuoding2"] = "承君恩宠，报效国家！",
  ["~kl__zhongyao"] = "墨尽，岁终。",
}

local guojia = General(extension, "kl__guojia", "wei", 3)
local kl__kuiji = fk.CreateTriggerSkill {
  name = "kl__kuiji",
  anim_type = "drawcard",
  events = { fk.FinishJudge },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.room:getCardArea(data.card) == Card.Processing
  end,
  on_use = function(self, event, target, player, data)
    player.room:obtainCard(player.id, data.card, true, fk.ReasonJustMove)
  end,
}
local kl__tiandu = fk.CreateTriggerSkill {
  name = "kl__tiandu",
  frequency = Skill.Compulsory, -- 锁定技标识[^2]
  events = { fk.EventPhaseStart },
  can_trigger = function(self, event, target, player, data)
    return target == player and
        player.phase == Player.Start and
        player:hasSkill(self)
  end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".|black",
    }
    -- 黑色判定牌处理逻辑[^3]
    room:judge(judge)
    if judge.card.color ~= Card.Red then
      room:damage {
        damage = 1,
        damageType = fk.NormalDamage,
        to = player,
        skillName = self.name,
      }
    end
    return true
  end
}
local kl__yiji = fk.CreateTriggerSkill {
  name = "kl__yiji",
  anim_type = "masochism",
  events = { fk.Damaged },
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for i = 1, data.damage do
      if i > 1 and (self.cancel_cost or not player:hasSkill(self)) then return false end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(target, self.name, data) then return true end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(2, self.name)
    if player.dead or player:isKongcheng() then return end
    room:askForYiji(player, player:getCardIds("he"), room:getOtherPlayers(player, false), self.name, 0, 2)
  end
}
guojia:addSkill(kl__tiandu)
guojia:addSkill(kl__kuiji)
guojia:addSkill(kl__yiji)
Fk:loadTranslationTable {
  ["kl__guojia"] = "郭嘉",
  ["#kl__guojia"] = "早终的先知",
  ["designer:kl__gaojia"] = "官方&抗绿先锋",

  ["kl__kuiji"] = "窥机",
  [":kl__kuiji"] = "当你的判定牌生效后，你可获得之。",
  ["kl__tiandu"] = "天妒",
  [":kl__tiandu"] = "锁定技，准备阶段，你判定，若结果为黑色，你受到1点伤害。",
  ["kl__yiji"] = "遗计",
  [":kl__yiji"] = "当你受到1点伤害后，你可摸两张牌，然后你可将至多两张牌交给一至两名其他角色。",
  ["#kl__yiji-give"] = "遗计：将至多%arg张牌分配给其他角色",


  ["$kl__kuiji1"] = "就这样吧。",
  ["$kl__kuiji2"] = "哦？",
  ["$kl__tiandu1"] = "嘉不受此劫，安能以凡人之躯窥得天机！",
  ["$kl__tiandu2"] = "九州为觞，风雨为酿，谁与我共饮此杯？",
  ["$kl__yiji1"] = "锦囊妙策，终定社稷。",
  ["$kl__yiji2"] = "依此计行，辽东可定。",
  ["~kl__guojia"] = "咳，咳……",
}

local caozhang = General(extension, "kl__caozhang", "wei", 4)
local kl__jiangchi = fk.CreateTriggerSkill {
  name = "kl__jiangchi",
  events = { fk.EventPhaseStart },
  can_trigger = function(self, event, target, player, data)
    return target == player and player.phase == Player.Play and player:hasSkill(self.name)
  end,

  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name)
  end,

  on_use = function(self, event, target, player, data)
    local room = player.room
    -- 摸两张牌
    player:drawCards(2, self.name)

    -- 计算可弃牌总数（手牌+装备）
    local hand_num = #player:getCardIds("h")
    local equip_num = #player:getCardIds("e")
    local totalCards = hand_num + equip_num

    -- 执行弃牌操作
    local discarded = room:askForDiscard(
      player, 0, totalCards, true, self.name,
      false, nil
    )

    -- 设置杀次数标记（X=弃牌数）
    local discard_num = #discarded
    room:setPlayerMark(player, "kl__jiangchi-turn", discard_num - 1)
    room:setPlayerMark(player, "@kl__jiangchi_attackrange-turn", discard_num)
  end
}
-- 目标修改技能：调整杀次数上限
local kl__jiangchi_mod = fk.CreateTargetModSkill {
  name = "#kl__jiangchi_mod",
  residue_func = function(self, player, skill, scope)
    if skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      return player:getMark("kl__jiangchi-turn")
    end
    return 0
  end,
}
local kl__jiangchi_attackrange = fk.CreateAttackRangeSkill {
  name = "#kl__jiangchi_attackrange",
  correct_func = function(self, player, to)
    return player:getMark("kl__jiangchi_attackrange-turn")
  end,
}
kl__jiangchi:addRelatedSkill(kl__jiangchi_mod)
kl__jiangchi:addRelatedSkill(kl__jiangchi_attackrange)
caozhang:addSkill(kl__jiangchi)
Fk:loadTranslationTable {
  ["kl__caozhang"] = "曹彰",
  ["#kl__caozhang"] = "黄须儿",
  ["designer:kl__caozhang"] = "官方&董太师",

  ["kl__jiangchi"] = "将驰",
  [":kl__jiangchi"] = "出牌阶段开始时，你可摸两张牌，弃置任意张牌。若如此做，本回合你使用【杀】的次数上限改为X、攻击范围+X（X为你弃置的牌数）。",

  ["@kl__jiangchi_attackrange-turn"] = "将驰：",

  ["$kl__jiangchi1"] = "谨遵父训，不可逞匹夫之勇。",
  ["$kl__jiangchi2"] = "吾定当身先士卒，振魏武雄风！",
  ["~kl__caozhang"] = "子桓，你害我！",
}

-- local xiahouyuan = General(extension, "kl__xiahouyuan", "wei", 4)
-- local kl__shensu = fk.CreateTriggerSkill {
--   name = "kl__shensu",             -- 技能名称
--   anim_type = "offensive",         -- 动画类型，表示这是一个进攻型技能
--   events = { fk.EventPhaseStart }, -- 触发时机：阶段开始时
--   can_trigger = function(self, event, target, player, data)
--     -- 判断技能是否可以触发
--     return target == player       -- 目标是当前玩家
--         and player:hasSkill(self) -- 玩家拥有该技能
--         and
--         (player.phase == Player.Judge or player.phase == Player.Draw or player.phase == Player.Play or player.phase == Player.Discard)
--         and not player:isAllNude()
--         and not (player:getMark("basic-turn") == 1 and player:getMark("equip-turn") == 1 and player:getMark("trick-turn") == 1)
--   end,
--   on_cost = function(self, event, target, player, data)
--     -- 技能的发动代价
--     local success, dat = player.room:askForUseActiveSkill(player, "kl__shensu_viewas",
--       "#kl__shensu-use", true, {
--         bypass_times = true,     -- 忽略使用次数限制
--         extraUse = true,         -- 允许额外使用
--         bypass_distances = true, -- 忽略距离限制
--       })
--     if success then
--       self.cost_data = dat -- 保存代价数据
--       return true          -- 返回技能可以发动
--     end
--   end,
--   on_use = function(self, event, target, player, data)
--     -- 技能的具体效果
--     local room = player.room -- 获取房间对象
--     local card = Fk.skills["kl__shensu_viewas"]:viewAs(self.cost_data.cards)
--     -- 将代价中的卡牌视为【杀】
--     room:useCard {
--       from = player.id, -- 使用者是当前玩家
--       tos = table.map(self.cost_data.targets, function(id)
--         return { id }   -- 技能目标
--       end),
--       card = card,      -- 使用的卡牌
--       extraUse = true,  -- 允许额外使用
--     }
--   end,
-- }
-- local kl__shensu_viewas = fk.CreateViewAsSkill {
--   name = "kl__shensu_viewas", -- 技能名称
--   expand_pile = function(self)
--     -- 将判定区的牌加入 expand_pile
--     return Self:getCardIds("j")
--   end,
--   card_filter = function(self, to_select, selected, player)
--     if player:getMark("basic-turn") == 0 and player:getMark("equip-turn") == 0 and player:getMark("trick-turn") == 0 then
--       return #selected == 0 and
--           (Self:getPileNameOfId(to_select) == "" or table.contains(Self:getCardIds("j"), to_select) or table.contains(Self:getHandlyIds(), to_select) or
--             table.contains(Self:getCardIds("e"), to_select))
--     end
--     if player:getMark("basic-turn") == 1 and player:getMark("equip-turn") == 0 and player:getMark("trick-turn") == 0 then
--       return #selected == 0 and
--           (Self:getPileNameOfId(to_select) == "" or table.contains(Self:getCardIds("j"), to_select) or table.contains(Self:getHandlyIds(), to_select) or
--             table.contains(Self:getCardIds("e"), to_select)) and
--           (Fk:getCardById(to_select).type == Card.TypeEquip or Fk:getCardById(to_select).type == Card.TypeTrick)
--     end
--     if player:getMark("basic-turn") == 0 and player:getMark("equip-turn") == 1 and player:getMark("trick-turn") == 0 then
--       return #selected == 0 and
--           (Self:getPileNameOfId(to_select) == "" or table.contains(Self:getCardIds("j"), to_select) or table.contains(Self:getHandlyIds(), to_select) or
--             table.contains(Self:getCardIds("e"), to_select)) and
--           (Fk:getCardById(to_select).type == Card.TypeBasic or Fk:getCardById(to_select).type == Card.TypeTrick)
--     end
--     if player:getMark("basic-turn") == 0 and player:getMark("equip-turn") == 0 and player:getMark("trick-turn") == 1 then
--       return #selected == 0 and
--           (Self:getPileNameOfId(to_select) == "" or table.contains(Self:getCardIds("j"), to_select) or table.contains(Self:getHandlyIds(), to_select) or
--             table.contains(Self:getCardIds("e"), to_select)) and
--           (Fk:getCardById(to_select).type == Card.TypeBasic or Fk:getCardById(to_select).type == Card.TypeEquip)
--     end
--     if player:getMark("basic-turn") == 1 and player:getMark("equip-turn") == 1 and player:getMark("trick-turn") == 0 then
--       return #selected == 0 and
--           (Self:getPileNameOfId(to_select) == "" or table.contains(Self:getCardIds("j"), to_select) or table.contains(Self:getHandlyIds(), to_select) or
--             table.contains(Self:getCardIds("e"), to_select)) and (Fk:getCardById(to_select).type == Card.TypeTrick)
--     end
--     if player:getMark("basic-turn") == 0 and player:getMark("equip-turn") == 1 and player:getMark("trick-turn") == 1 then
--       return #selected == 0 and
--           (Self:getPileNameOfId(to_select) == "" or table.contains(Self:getCardIds("j"), to_select) or table.contains(Self:getHandlyIds(), to_select) or
--             table.contains(Self:getCardIds("e"), to_select)) and (Fk:getCardById(to_select).type == Card.TypeBasic)
--     end
--     if player:getMark("basic-turn") == 1 and player:getMark("equip-turn") == 0 and player:getMark("trick-turn") == 1 then
--       return #selected == 0 and
--           (Self:getPileNameOfId(to_select) == "" or table.contains(Self:getCardIds("j"), to_select) or table.contains(Self:getHandlyIds(), to_select) or
--             table.contains(Self:getCardIds("e"), to_select)) and (Fk:getCardById(to_select).type == Card.TypeEquip)
--     end
--   end,
--   view_as = function(self, cards)
--     -- 将选中的牌视为另一种牌
--     if #cards ~= 1 then return end              -- 必须且只能选择一张牌
--     local card = Fk:cloneCard("thunder__slash") -- 创建一张【杀】牌
--     card.skillName = "kl__shensu"               -- 设置技能名称为“kl__shensu”
--     card:addSubcard(cards[1])                   -- 将选中的牌作为子牌附加到【杀】牌上
--     return card                                 -- 返回生成的【杀】牌
--   end,
--   before_use = function(self, player, use)
--     local room = player.room
--     if Fk:getCardById(use.card.subcards[1]).type == Card.TypeEquip then
--       room:setPlayerMark(player, "equip-turn", 1)
--       room:sendLog { type = "#ceshi" }
--     elseif Fk:getCardById(use.card.subcards[1]).type == Card.TypeBasic then
--       room:setPlayerMark(player, "basic-turn", 1)
--       room:sendLog { type = "#ceshi" }
--     elseif Fk:getCardById(use.card.subcards[1]).type == Card.TypeTrick then
--       room:setPlayerMark(player, "trick-turn", 1)
--       room:sendLog { type = "#ceshi" }
--     end
--   end,
-- } --bug:before_use无法获取标记
-- Fk:addSkill(kl__shensu_viewas)
-- xiahouyuan:addSkill(kl__shensu)
-- Fk:loadTranslationTable {
--   ["kl__xiahouyuan"] = "夏侯渊",
--   ["#kl__xiahouyuan"] = "疾行的猎豹",
--   ["designer:kl__xiahouyuan"] = "官方&抗绿先锋",

--   ["#ceshi"] = "测试",
--   ["kl__shensu"] = "神速",
--   ["kl__shensu_viewas"] = "神速",
--   ["kl__shensu_use"] = "你可将区域内一张牌当无距离限制的雷【杀】使用",
--   [":kl__shensu"] = "每回合每类别限一次，你的阶段开始时，你可将区域内一张牌当无距离限制的雷【杀】使用（准备和结束阶段除外）。",

--   ["$kl__shensu1"] = "吾等无需恋战",
--   ["$kl__shensu2"] = "吾自当以一当十速战速决",
--   ["~kl__xiahouyuan"] = "若非中其奸计，吾岂会命丧贼手……",
-- }

local guohuai = General(extension, "kl__guohuai", "wei", 4)
local kl__jingce = fk.CreateTriggerSkill {
  name = "kl__jingce",
  events = { fk.EventPhaseEnd },
  can_trigger = function(self, event, target, player, data)
    if target.phase == Player.Finish and player:hasSkill(self.name) then
      local room = player.room
      -- 取当前回合标记作为比对基准 [^0]
      local flag = "jingce_counter-turn" .. player.id
      return player:getMark(flag) >= player.hp
    end
  end,
  on_use = function(self, event, target, player, data)
    -- local room = player.room
    -- local flag = "jingce_counter-turn" .. player.id
    -- room:setPlayerMark(player, flag, 0)
    player:drawCards(2)
  end
}
-- 卡牌计数监听技能作为辅助
local kl__jingce_counter = fk.CreateTriggerSkill {
  name = "#kl__jingce_counter",
  events = { fk.CardUsing, fk.CardResponding },
  main_skill = kl__jingce,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(kl__jingce.name)
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    -- 回合标记动态更新 [^0]
    local flag = "jingce_counter-turn" .. player.id
    room:addPlayerMark(player, flag, 1)
  end,
}
kl__jingce:addRelatedSkill(kl__jingce_counter)
guohuai:addSkill(kl__jingce)
Fk:loadTranslationTable {
  ["kl__guohuai"] = "郭淮",
  ["#kl__guohuai"] = "垂问秦雍",
  ["designer:kl__guohuai"] = "官方&抗绿先锋",

  ["kl__jingce"] = "精策",
  [":kl__jingce"] = "每名角色的结束阶段，若你本回合使用或打出的牌数不小于X，你可摸两张牌（X为你的体力值）。",
  ["#kl__jingce_counter"] = "精策",

  ["$kl__jingce1"] = "方策精详，有备无患。",
  ["$kl__jingce2"] = "精兵据敌，策守如山。",
  ['~kl__guohuai'] = '姜维小儿，竟然……',
}

local caoang = General(extension, "kl__caoang", "wei", 4)
local kl__kangkai = fk.CreateTriggerSkill {
  name = "kl__kangkai",
  anim_type = "support",
  events = { fk.TargetConfirmed },
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and data.card.is_damage_card and
        (target == player or player:distanceTo(target) == 1)
  end,
  on_cost = function(self, event, target, player, data)
    local prompt = (player.id == data.to) and "#kl__kangkai-self" or "#kl__kangkai-invoke::" .. data.to
    return player.room:askForSkillInvoke(player, self.name, nil, prompt)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(data.to)
    player:drawCards(1, self.name)
    if player == to or player:isNude() or to.dead then return end
    local cards = room:askForCard(player, 1, 1, true, self.name, false, ".", "#kl__kangkai-give::" .. to.id)
    if #cards > 0 then
      room:moveCardTo(cards, Card.PlayerHand, to, fk.ReasonGive, self.name, nil, true, player.id)
      to:showCards(cards)
      local card = Fk:getCardById(cards[1])
      if card.type == Card.TypeEquip and not to.dead and not to:isProhibited(to, card) and not to:prohibitUse(card) and
          table.contains(to:getCardIds("h"), cards[1]) and
          room:askForSkillInvoke(to, self.name, data, "#kl__kangkai-use:::" .. card:toLogString()) then
        room:useCard({
          from = to.id,
          tos = { { to.id } },
          card = card,
        })
      end
    end
  end,
}
caoang:addSkill(kl__kangkai)
Fk:loadTranslationTable {
  ["kl__caoang"] = "曹昂",
  ["#kl__caoang"] = "取义成仁",
  ["designer:kl__caoang"] = "官方&抗绿先锋",

  ["kl__kangkai"] = "慷忾",
  [":kl__kangkai"] = "当一名角色成为伤害牌的目标后，若你至其的距离不大于1，你可摸一张牌。若如此做，你先将一张牌交给该角色再令其展示之，" ..
      "若此牌为装备牌，其可使用之。",
  ["#kl__kangkai-invoke"] = "慷忾：你可摸一张牌，再交给 %dest 一张牌",
  ["#kl__kangkai-self"] = "慷忾：你可摸一张牌",
  ["#kl__kangkai-give"] = "慷忾：选择一张牌交给 %dest",
  ["#kl__kangkai-use"] = "慷忾：你可使用%arg",

  ["$kl__kangkai1"] = "典将军，比比看谁杀敌更多！",
  ["$kl__kangkai2"] = "父亲快走，有我殿后！",
  ["~kl__caoang"] = "典将军，还是你赢了……",
}

local caochong = General(extension, "kl__caochong", "wei", 3)
Fk:addPoxiMethod {
  name = "kl__chengxiang_count",
  card_filter = function(to_select, selected, data)
    if table.contains(data[2], to_select) then return true end
    local n = Fk:getCardById(to_select).number
    for _, id in ipairs(data[2]) do
      n = n + Fk:getCardById(id).number
    end
    return n < 14
  end,
  feasible = function(selected)
    return true
  end,
}
local kl__chengxiang = fk.CreateTriggerSkill {
  name = "kl__chengxiang",
  anim_type = "masochism",
  events = { fk.Damaged },
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for _ = 1, data.damage do
      if self.cancel_cost or not player:hasSkill(self) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(4)
    room:moveCards({
      ids = cards,
      toArea = Card.Processing,
      moveReason = fk.ReasonPut,
      proposer = player.id,
      skillName = self.name,
    })
    local get = room:askForArrangeCards(player, self.name, { cards },
      "#kl__chengxiang-choose", false, 0, { 4, 4 }, { 0, 1 }, ".", "kl__chengxiang_count", { {}, { cards[1] } })[2]
    room:moveCardTo(get, Player.Hand, player, fk.ReasonJustMove, self.name, "", true, player.id)
    cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.Processing end)
    if #cards > 0 then
      room:moveCards({
        ids = cards,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonPutIntoDiscardPile,
      })
    end
  end
}
local kl__renxin = fk.CreateTriggerSkill {
  name = "kl__renxin",
  anim_type = "support",
  events = { fk.DamageInflicted },
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target ~= player and data.damage >= target.hp and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local card = room:askForDiscard(player, 1, 1, true, self.name, true, ".|.|.|.|.|equip",
      "#kl__renxin-invoke::" .. target.id,
      true)
    if #card > 0 then
      self.cost_data = card
      return true
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    player.room:throwCard(self.cost_data, self.name, player, player)
    player:turnOver()
    return true
  end,
}
caochong:addSkill(kl__chengxiang)
caochong:addSkill(kl__renxin)
Fk:loadTranslationTable {
  ["kl__caochong"] = "曹冲",
  ["#kl__caochong"] = "仁爱的神童",
  ["designer:kl__caochong"] = "官方&抗绿先锋",

  ["kl__renxin"] = "仁心",
  [":kl__renxin"] = "当一名其他角色受到致命伤害时，你可弃置一张装备牌，将武将牌翻面，防止此伤害。",
  ["#kl__renxin-invoke"] = "仁心：你可弃置一张装备牌，防止 %dest 受到的致命伤害",
  ["#kl__chengxiang-choose"] = "称象：获得任意点数之和小于或等于13的牌",
  ["kl__chengxiang"] = "称象",
  [":kl__chengxiang"] = "当你受到1点伤害后，你可亮出牌堆顶四张牌，获得其中任意张点数之和不大于13的牌，将其余的牌置入弃牌堆。",

  ["$kl__chengxiang1"] = "依我看，小事一桩。",
  ["$kl__chengxiang2"] = "孰重孰轻，一称便知。",
  ["$kl__renxin1"] = "仁者爱人，人恒爱之。",
  ["$kl__renxin2"] = "有我在，别怕。",
  ["~kl__caochong"] = "子桓哥哥……",
}

local zhonghui = General(extension, "kl__zhonghui", "wei", 4)
local kl__quanji = fk.CreateTriggerSkill {
  name = "kl__quanji",
  anim_type = "masochism",
  events = { fk.Damaged },
  derived_piles = { "kl__zhonghui_quan" },
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for i = 1, data.damage do
      if self.cancel_cost or player.dead then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askForSkillInvoke(player, self.name, data) then
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1, self.name)
    if player:isKongcheng() then return end
    local card = room:askForCard(player, 1, 1, true, self.name, false, ".", "#kl__quanji-card")
    player:addToPile("kl__zhonghui_quan", card, true, self.name)
  end,
}
local kl__quanji_maxcards = fk.CreateMaxCardsSkill {
  name = "#kl__quanji_maxcards",
  correct_func = function(self, player)
    if player:hasSkill(self) then
      return #player:getPile("kl__zhonghui_quan")
    else
      return 0
    end
  end,
}
local kl__paiyi = fk.CreateViewAsSkill {
  name = "kl__paiyi",
  anim_type = "control",
  pattern = "snatch",
  expand_pile = "kl__zhonghui_quan",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Self:getPileNameOfId(to_select) == "kl__zhonghui_quan"
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then
      return nil
    end
    local c = Fk:cloneCard("ex_nihilo")
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
}
kl__quanji:addRelatedSkill(kl__quanji_maxcards)
zhonghui:addSkill(kl__quanji)
zhonghui:addSkill(kl__paiyi)
Fk:loadTranslationTable {
  ["kl__zhonghui"] = "钟会",
  ["#kl__zhonghui"] = "桀骜的野心家",
  ["designer:kl__zhonghui"] = "官方&江奕",

  ["kl__quanji"] = "权计",
  [":kl__quanji"] = "当你受到1点伤害后，你可摸一张牌，然后将一张牌置于武将牌上，称为\"权\"；" ..
      "你的手牌上限+X（X为\"权\"数）。",
  ["kl__zhonghui_quan"] = "权",
  ["#kl__quanji-card"] = "权计：将一张牌置为\"权\"",
  ["kl__paiyi"] = "排异",
  [":kl__paiyi"] = "你可将一张\"权\"当【无中生有】使用。",

  ["$kl__quanji1"] = "这仇，我记下了。",
  ["$kl__quanji2"] = "先让你得意几天。",
  ["$kl__paiyi1"] = "妨碍我的人，都得死！",
  ["$kl__paiyi2"] = "此地容不下你！",
  ["~kl__zhonghui"] = "伯约，让你失望了。",
}

local wangyi = General(extension, "kl__wangyi", "wei", 3, 3, General.Female)
local kl__zhenlie = fk.CreateTriggerSkill {
  name = "kl__zhenlie",
  anim_type = "defensive",
  events = { fk.TargetConfirming, fk.BeforeCardsMove },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.from ~= player.id and
        (data.card.trueName == "slash" or data.card.type == Card.TypeTrick)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:loseHp(player, 1, self.name)
    if player.dead then return end
    table.insertIfNeed(data.nullifiedTargets, player.id)
    local to = room:getPlayerById(data.from)
    if to.dead or to:isNude() then return end
    local id = room:askForCardChosen(player, to, "he", self.name)
    room:throwCard({ id }, self.name, to, player)
  end,
}
local kl__miji = fk.CreateTriggerSkill {
  name = "kl__miji",
  anim_type = "drawcard",
  events = { fk.EventPhaseStart },
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Finish and player:isWounded()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local n = player:getLostHp()
    player:drawCards(n, self.name)
    if player:getHandcardNum() >= n and room:askForSkillInvoke(player, self.name, nil, "#kl__miji-invoke:::" .. n) then
      room:askForYiji(player, player:getCardIds("he"), room:getOtherPlayers(player), self.name, 1, n)
    end
  end,
}
wangyi:addSkill(kl__zhenlie)
wangyi:addSkill(kl__miji)
Fk:loadTranslationTable {
  ["kl__wangyi"] = "王异",
  ["#kl__wangyi"] = "决意的巾帼",
  ["designer:kl__wangyi"] = "官方&抗绿先锋",

  ["kl__zhenlie"] = "贞烈",
  [":kl__zhenlie"] = "当你成为其他角色使用【杀】或锦囊牌的目标时，你可失去1点体力取消之，然后你弃置其一张牌。",
  ["kl__miji"] = "秘计",
  [":kl__miji"] = "结束阶段，你可摸X张牌，然后可将至多等量张牌分配给其他角色（X为你已损失的体力值）。",
  ["kl__miji_active"] = "秘计",
  ["#kl__miji-invoke"] = "秘计：是否将 %arg 张牌分配给其他角色",

  ["$kl__zhenlie1"] = "虽是妇人，亦当奋身一搏！",
  ["$kl__zhenlie2"] = "为雪前耻，不惜吾身！",
  ["$kl__miji1"] = "此计，可歼敌精锐！",
  ["$kl__miji2"] = "此举，可破敌之围！",
  ["~kl__wangyi"] = "月儿，不要责怪你爹爹……",
}
-----------------------------------------------------------------------
local dengai = General(extension, "kl__dengai", "wei", 4)
local kl__tuntian = fk.CreateTriggerSkill {
  name = "kl__tuntian",
  anim_type = "special",
  derived_piles = "kl__dengai_field",
  events = { fk.AfterCardsMove },
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player.phase ~= Player.Play then
      for _, move in ipairs(data) do
        if move.from == player.id then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = self.name,
      pattern = ".|.|spade,club,diamond",
    }
    room:judge(judge)
  end,

  refresh_events = { fk.FinishJudge },
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(self) and data.reason == self.name
  end,
  on_refresh = function(self, event, target, player, data)
    if data.card.suit ~= Card.Heart and player.room:getCardArea(data.card) == Card.Processing then
      player:addToPile("kl__dengai_field", data.card, true, self.name)
    end
  end,
}
local kl__tuntian_distance = fk.CreateDistanceSkill {
  name = "#kl__tuntian_distance",
  correct_func = function(self, from, to)
    if from:hasSkill(self) then
      return - #from:getPile("kl__dengai_field")
    end
  end,
}
local kl__jixi = fk.CreateViewAsSkill { --FIXME: 用来急袭的那张田不应产生-1距离
  name = "kl__jixi",
  anim_type = "control",
  pattern = "snatch",
  expand_pile = "kl__dengai_field",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Self:getPileNameOfId(to_select) == "kl__dengai_field"
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then
      return nil
    end
    local c = Fk:cloneCard("snatch")
    c.skillName = self.name
    c:addSubcard(cards[1])
    return c
  end,
}
kl__tuntian:addRelatedSkill(kl__tuntian_distance)
dengai:addSkill(kl__tuntian)
dengai:addSkill(kl__jixi)
Fk:loadTranslationTable {
  ["kl__dengai"] = "邓艾",
  ["#kl__dengai"] = "矫然的壮士",
  ["designer:kl__dengai"] = "官方&抗绿先锋",

  ["kl__tuntian"] = "屯田",
  [":kl__tuntian"] = "当你于出牌阶段外失去牌后，你可判定：若结果不为<font color='red'>♥</font>，你将生效后的判定牌置于你的武将牌上，称为\"田\"；" ..
      "你计算至其他角色的距离-X（X为\"田\"数）。",
  ["kl__jixi"] = "急袭",
  [":kl__jixi"] = "你可将一张\"田\"当【顺手牵羊】使用。",
  ["kl__dengai_field"] = "田",

  ["$kl__tuntian1"] = "休养生息，备战待敌。",
  ["$kl__tuntian2"] = "锄禾日当午，汗滴禾下土。",
  ["$kl__jixi1"] = "偷渡阴平，直取蜀汉！",
  ["$kl__jixi2"] = "攻其无备，出其不意！",
  ["~kl__dengai"] = "吾破蜀克敌，竟葬于奸贼之手！",
}

local wangchang = General(extension, "kl__wangchang", "wei", 3)
local kl__bingshi = fk.CreateActiveSkill {
  name = "kl__bingshi",
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return #selected == 0 and to_select ~= Self.id and
        -- Self:inMyAttackRange(Fk:currentRoom():getPlayerById(to_select))
        (#target:getCardIds { Player.Hand, Player.Equip } + #Self:getCardIds { Player.Hand, Player.Equip } >= 2)
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local card_data = {}
    if #target:getCardIds(Player.Hand) > 0 then
      local handcards = {}
      for _ = 1, #target:getCardIds(Player.Hand), 1 do
        table.insert(handcards, -1)
      end
      table.insert(card_data, { "needhand", handcards })
    end
    if #target:getCardIds(Player.Equip) > 0 then
      table.insert(card_data, { "needequip", target:getCardIds(Player.Equip) })
    end
    if #player:getCardIds(Player.Hand) > 0 then
      table.insert(card_data, { "wordhand", player:getCardIds(Player.Hand) })
    end
    if #player:getCardIds(Player.Equip) > 0 then
      table.insert(card_data, { "wordequip", player:getCardIds(Player.Equip) })
    end
    local cards = room:askForCardsChosen(player, target, 2, 2, { card_data = card_data }, self.name) -- 改为选2张
    local cards1 = table.filter(cards,
      function(id) return table.contains(player:getCardIds { Player.Hand, Player.Equip }, id) end)
    local cards2 = table.filter(cards,
      function(id) return table.contains(target:getCardIds { Player.Hand, Player.Equip }, id) end)

    local moveInfos = {}
    if #cards1 > 0 then
      table.insert(moveInfos, {
        from = player.id,
        ids = cards1,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonDiscard,
        proposer = effect.from,
        skillName = self.name,
      })
    end
    if #cards2 > 0 then
      table.insert(moveInfos, {
        from = target.id,
        ids = cards2,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonDiscard,
        proposer = effect.from,
        skillName = self.name,
      })
    end
    room:moveCards(table.unpack(moveInfos))

    if not player.dead and not target.dead then
      if #cards1 == 2 then -- 伤害条件改为2张
        room:damage {
          from = player,
          to = target,
          damage = 1,
          skillName = self.name,
        }
      elseif #cards2 == 2 then -- 伤害条件改为2张
        room:damage {
          from = target,
          to = player,
          damage = 1,
          skillName = self.name,
        }
      end
    end
  end,
}
local kl__pitian = fk.CreateTriggerSkill {
  name = "kl__pitian",
  anim_type = "drawcard",
  events = { fk.Damaged, fk.EventPhaseStart },
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self.name) then return false end
    if event == fk.Damaged then
      return target == player
    elseif event == fk.EventPhaseStart then
      return target == player and player.phase == Player.Finish and player:getMark("@pitian") > 0
    end
    return false
  end,
  on_trigger = function(self, event, target, player, data)
    local room = player.room
    if event == fk.Damaged then
      for _ = 1, data.damage do
        room:addPlayerMark(player, "@pitian", 1)
      end
    else
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      return player.room:askForSkillInvoke(player, self.name, nil, "#kl__pitian-invoke")
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local max = player:getMaxCards()
    local hand = player:getHandcardNum()
    if hand < max then
      player:drawCards(max - hand, self.name)
      room:setPlayerMark(player, "@pitian", 0)
    end
  end,
}
local kl__pitian_maxcards = fk.CreateMaxCardsSkill {
  name = "#kl__pitian_maxcards",
  correct_func = function(self, player)
    return player:getMark("@pitian")
  end,
}
kl__pitian:addRelatedSkill(kl__pitian_maxcards)
wangchang:addSkill(kl__bingshi)
wangchang:addSkill(kl__pitian)
Fk:loadTranslationTable {
  ["kl__wangchang"] = "王昶",
  ["#kl__wangchang"] = "民安寇灭",
  ["designer:kl__wangchang"] = "官方&抗绿先锋",

  ["kl__bingshi"] = "兵势",
  [":kl__bingshi"] = "出牌阶段限一次，你可弃置你和一名其他角色共计两张牌。若有角色因此失去两张牌，其对另一名角色造成1点伤害。",
  ["kl__pitian"] = "辟田",
  [":kl__pitian"] = "当你受到1点伤害后，你可令你的手牌上限+1。结束阶段，若你因此技能增加了手牌上限，你可将手牌摸至手牌上限，然后重置因此技能而增加的手牌上限。",

  ["#bingshi-choose"] = "兵势：弃置双方共计两张牌",
  ["@pitian"] = "辟田",
  ["#kl__pitian-invoke"] = "辟田：你可将手牌摸至手牌上限，然后重置本技能增加的手牌上限",

  ["wordhand"] = "我的手牌",
  ["wordequip"] = "我的装备",
  ["needhand"] = "对方手牌",
  ["needequip"] = "对方装备",

  ["$kl__bingshi1"] = "吾民在后，岂惧尔等魍魉。",
  ["$kl__bingshi2"] = "凶兵来袭，当长戈相迎。",
  ["$kl__pitian1"] = "此间辟地数旬，必成良田千亩。",
  ["$kl__pitian2"] = "民以物力为天，物力唯田可得。",
  ["~kl__wangchang"] = "此世为官，未辱青天之名……",
}

-- local lidian = General(extension, "kl__lidian", "wei", 3)
-- local kl__xunxun = fk.CreateTriggerSkill {
--   name = "kl__xunxun",
--   anim_type = "control",
--   events = { fk.EventPhaseStart },
--   can_trigger = function(self, event, target, player, data)
--     return target == player and player:hasSkill(self) and player.phase == Player.Draw
--   end,
--   on_use = function(self, event, target, player, data)
--     local room = player.room
--     local ret = room:askForArrangeCards(player, self.name, { room:getNCards(4), "Bottom", "Top" }, "#kl__xunxun", true, 0,
--       { 4, 2 }, { 0, 2 })
--     local top, bottom = ret[2], ret[1]
--     for i = #top, 1, -1 do
--       table.removeOne(room.draw_pile, top[i])
--       table.insert(room.draw_pile, 1, top[i])
--     end
--     for i = 1, #bottom, 1 do
--       table.removeOne(room.draw_pile, bottom[i])
--       table.insert(room.draw_pile, bottom[i])
--     end
--     room:sendLog {
--       type = "#GuanxingResult",
--       from = player.id,
--       arg = #top,
--       arg2 = #bottom,
--     }
--   end,
-- }
-- local kl__wangxi = fk.CreateTriggerSkill {
--   name = "kl__wangxi",
--   events = { fk.Damage, fk.Damaged },
--   mute = true,
--   can_trigger = function(self, event, target, player, data)
--     return target == player and player:hasSkill(self) and data.from and data.from ~= data.to and
--         not (data.from.dead or data.to.dead)
--   end,
--   on_trigger = function(self, event, target, player, data)
--     self.cancel_cost = false
--     for i = 1, data.damage do
--       if self.cancel_cost then break end
--       if not self:triggerable(event, target, player, data) then break end
--       self:doCost(event, target, player, data)
--     end
--   end,
--   on_cost = function(self, event, target, player, data)
--     local to = event == fk.Damage and data.to or data.from
--     if player.room:askForSkillInvoke(player, self.name, nil, "#kl__wangxi-invoke::" .. to.id) then
--       player.room:doIndicate(player.id, { to.id })
--       return true
--     end
--     self.cancel_cost = true
--   end,
--   on_use = function(self, event, target, player, data)
--     local room = player.room
--     player:broadcastSkillInvoke(self.name)
--     room:notifySkillInvoked(player, self.name, event == fk.Damaged and "masochism" or "drawcard")
--     local cards = room:drawCards(player, 2, self.name)
--     local to = event == fk.Damage and data.to or data.from
--     if player.dead or player:isNude() or to.dead then return false end
--     -- local id1 = Fk:getCardById(cards[1])
--     -- local id2 = Fk:getCardById(cards[2])
--     -- room:drawCards(player, 1, self.name)
--     -- room:drawCards(to, 1, self.name)
--     -- room:askForYiji(player, cards, to, self.name, 1, 1)
--     local card = room:askForCard(player, 1, 1, true, self.name, false, ".", "#kl__wangxi-give::" .. to.id)
--     if #card > 0 then
--       room:moveCardTo(card, Card.PlayerHand, to, fk.ReasonGive, self.name, nil, false, player.id)
--     end
--   end
-- }
-- lidian:addSkill(kl__xunxun)
-- lidian:addSkill(kl__wangxi)
-- Fk:loadTranslationTable {
--   ["kl__lidian"] = "李典",
--   ["#kl__lidian"] = "深明大义",
--   ["designer:kl__lidian"] = "官方",

--   ["kl__xunxun"] = "恂恂",
--   [":kl__xunxun"] = "摸牌阶段开始时，你可观看牌堆顶的四张牌，将其中两张牌以任意顺序置于牌堆顶，其余以任意顺序置于牌堆底。",
--   ["kl__wangxi"] = "忘隙",
--   [":kl__wangxi"] = "当你对其他角色造成1点伤害后，或当你受到其他角色造成的1点伤害后，若其存活，你可摸两张牌，交给其一张牌。",
--   ["#kl__wangxi-invoke"] = "是否对 %dest 发动 忘隙",
--   ["#kl__wangxi-give"] = "忘隙：选择一张牌交给 %dest",

--   ["$kl__xunxun1"] = "众将死战，非我之功。",
--   ["$kl__xunxun2"] = "爱兵如子，胜乃可全。",
--   ["$kl__wangxi1"] = "大丈夫，何拘小节。",
--   ["$kl__wangxi2"] = "前尘往事，莫再提起。",
--   ["~kl__lidian"] = "报国杀敌，虽死犹荣……",
-- }

return extension
